﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class CategoryController : AdminBaseController
    {
        public ActionResult GetOne(int id)
        {
            if (!HasQx("category"))
            {
                return Error("没有权限！");
            }
            return Success(new categoryCore().GetOne(id));
        }
        public ActionResult GetTree()
        {
            if (!HasQx("category"))
            {
                return Error("没有权限！");
            }
            return Success(new categoryCore().GetTree());
        }
        public ActionResult ClearTree()
        {
            if (!HasQx("category"))
            {
                return Error("没有权限！");
            }
            new categoryCore().ClearTreeCache();
            return Success("清理完成！");
        }

        [HttpPost]
        public ActionResult SaveOrdnum(List<categoryEntity> categorylist)
        {
            if (!HasQx("category"))
            {
                return Error("没有权限！");
            }
            var core = new categoryCore();
            foreach (var categoryEntity in categorylist)
            {
                core.Update(new { categoryEntity.ordnum }, categoryEntity.ID);
                core.ClearTreeCache();
            }
            return Success();
        }
        [HttpPost]
        public ActionResult Del(int id)
        {
            if (!HasQx("category"))
            {
                return Error("没有权限！");
            }
            var core = new categoryCore();
            if (id > 0)
            {
                if (core.Del(id))
                {
                    core.ClearTreeCache();
                    Log(curmanage.username + "删除分类ID=" + id);
                    return Success("删除成功！");
                }

            }
            return Error("删除失败！");
        }
        [HttpPost]
        public ActionResult DelAll(List<categoryEntity> modelList)
        {
            if (!HasQx("category"))
            {
                return Error("没有权限！");
            }
            var core = new categoryCore();
            string ids = string.Empty;
            foreach (var categoryEntity in modelList)
            {
                core.Del(categoryEntity);
                ids += categoryEntity.ID + ",";
            }
            core.ClearTreeCache();
            ids = ids.TrimEnd(',');
            Log(curmanage.username  + "删除分类ID in (" + ids + ")");
            return Success("删除成功！");
        }
        [HttpPost]
        public ActionResult Save(categoryEntity category)
        {
            if (!HasQx("category"))
            {
                return Error("没有权限！");
            }
            var core = new categoryCore();
            if (category.title == null)
            {
                return Error();
            }

            if (category.ID == 0)
            {
                category.sid = 1;//TODO 多站点调整
                category.mid = curmanage.ID;
                category.addtime=DateTime.Now;
                category.updatetime=DateTime.Now;
            }
            var model = core.Save(category);
            core.ClearTreeCache();
            Log(curmanage.username + "保存分类" + model.ToJson());
            return Success(model);
        }
    }
}
